package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.comm.pojo.EasyUIDataGridResult;
import com.taotao.dao.TbItemDescMapper;
import com.taotao.dao.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.ItemService;
import com.taotao.utils.IDUtils;
import com.taotao.utils.TaotaoResult;

@Transactional
@Service
public class ItemServiceImpl implements ItemService{
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;

	@Override
	public EasyUIDataGridResult getTbItems(int page,int rows) {
		PageHelper.startPage(page, rows);
		
		TbItemExample example = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(example);
		
		//取分页信息
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		
		EasyUIDataGridResult easyUIDataGridResult = new EasyUIDataGridResult();
		
		easyUIDataGridResult.setRows(list);
		easyUIDataGridResult.setTotal(pageInfo.getTotal());
		
		return easyUIDataGridResult;
	}

	@Override
	public TaotaoResult saveTbItem(TbItem item,String desc) {
		Date date = new Date();
		long id = IDUtils.genItemId();
		
		
		item.setCreated(date);
		item.setId(id);
		//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		item.setUpdated(date);
		
		TbItemDesc itemDesc = new TbItemDesc();
		itemDesc.setCreated(date);
		itemDesc.setItemDesc(desc);
		itemDesc.setItemId(id);
		itemDesc.setUpdated(date);
		
		itemMapper.insert(item);
		itemDescMapper.insert(itemDesc);

		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult editTBitem(TbItem item, String desc) {
		Long id = item.getId();
		Date date = new Date();
		item.setUpdated(date);
		TbItem selectByPrimaryKey = itemMapper.selectByPrimaryKey(id);
		selectByPrimaryKey.setCid(item.getCid());
		selectByPrimaryKey.setTitle(item.getTitle());
		selectByPrimaryKey.setSellPoint(item.getSellPoint());
		selectByPrimaryKey.setPrice(item.getPrice());
		selectByPrimaryKey.setNum(item.getNum());
		selectByPrimaryKey.setBarcode(item.getBarcode());
		selectByPrimaryKey.setImage(item.getImage());
		selectByPrimaryKey.setUpdated(date);
		itemMapper.updateByPrimaryKey(selectByPrimaryKey);
		
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
		itemDesc.setItemDesc(desc);
		itemDesc.setUpdated(date);
		itemDescMapper.updateByPrimaryKey(itemDesc);
		
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult changeStatus(TbItem item,String ids) {
		String[] split = ids.split(",");
		for (String string : split) {
			long id = Long.parseLong(string);
			item.setId(id);
			itemMapper.updateStatus(item);
		}
		return TaotaoResult.ok();
	}

	
	
	

	
	
	
}
